#include <iostream>
#include <jsoncpp/json/json.h>
#include <memory>
#include <sstream>
bool serialize(const Json::Value& val,std::string& body)
{
    std::stringstream ss;
    Json::StreamWriterBuilder swb;
    std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
    int ret =sw->write(val,&ss);
    if(ret!=0)
    {
        std::cout<<"json serialize failed"<<std::endl;
        return false;
    }
    body=ss.str();
    return true;
}
bool unserialize(std::string& val, Json::Value& body)
{
    Json::CharReaderBuilder jcb;
    std::unique_ptr<Json::CharReader> cr(jcb.newCharReader());
    Json::String error;
    bool ret = cr->parse(val.c_str(),val.c_str()+val.size(),&body,&error);
    if(!ret)
    {
     std::cout<<error<<std::endl;
     return false;
    }
    return true;
}
int main()
{
    Json::Value student;
    student["name"]="zhangsan";
    student["sex"]="man";
    student["age"]=18;
    student["socre"].append(110);
    student["socre"].append(18);
    student["socre"].append(19);
    Json::Value hobby;
    hobby["book"]="women";
    hobby["sport"]="swim";
    student["hobby"]=hobby;
    std::string str;
    serialize(student,str);
    std::cout<<str<<"\n";
    std::string va=R"({"name":"xiaohei","age":15,"score":[13,15,10]})";
    Json::Value stu;
    unserialize(va,stu);
    std::cout<<"name"<<stu["name"]<<"\n";
    std::cout<<"age"<<stu["age"]<<"\n";
    std::cout<<"score"<<stu["score"][2]<<"\n";
    return 0;
}